package org.zerofwk.example.datastruct.queue;

/**
 * desc:
 *
 * @author: Administrator
 * @date: 2021/4/28 9:38
 */
public class ArrayQueueTest {

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(5);
        //添加测试
        queue.add(3);
        queue.add(5);
        queue.add(6);
        queue.add(7);
        queue.add(8);
        queue.add(18);

        queue.get();
        queue.get();
        queue.get();
        queue.get();
        queue.get();

        queue.add(19);
        try {
            queue.show();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    static class ArrayQueue {
        private int maxSize;
        private int front;
        private int rear;
        private int[] arr;

        public ArrayQueue(int maxSize) {
            this.maxSize = maxSize;
            front = -1;
            rear = -1;
            arr = new int[maxSize];
        }

        public boolean isFull() {
            return front == maxSize - 1;
        }

        public boolean isEmpty() {
            return front == rear;
        }

        public void add(int pElem) {
            if (isFull()) {
                System.out.printf("数组满，无法添加:[%d]\n", pElem);
                return;
            }
            front++;
            arr[front] = pElem;
        }

        public int get() {
            if (isEmpty()) {
                throw new RuntimeException("get()->数组是空的...");
            }
            rear++;
            int val = arr[rear];
            System.out.printf("取出的数据是:arr[%d]=%d\n", rear, val);
            return val;
        }

        public void show() {
            if (isEmpty()) {
                throw new RuntimeException("show()->数组是空的...");
            }
            for (int i = rear + 1; i < maxSize; i++) {
                System.out.printf("arr[%d]=%d %s", i, arr[i], ",");
            }
        }

    }
}


